const fs = require("fs");
const path = require("path");
const React = require("react");
const { removeDir, getHash } = require("./utils");
function compileIndexTemplate(indexTemplatePath, nav) {
  const indexTemplate = fs.readFileSync(indexTemplatePath, "utf-8");
  const { code } = require("@babel/core").transform(indexTemplate, {
    presets: ["@babel/preset-react"],
    plugins: ["@babel/plugin-transform-modules-commonjs"]
  });
  const filename = getHash(code, "utf8", "md5").slice(0, 8);
  const tmpPath = path.resolve(process.cwd(), "./__azirbooktmp__");
  if (fs.existsSync(tmpPath)) {
    removeDir(tmpPath);
  }
  fs.mkdirSync(tmpPath);

  const writeStream = fs.createWriteStream(
    path.join(tmpPath, `./${filename}.js`),
    {
      emitClose: true
    }
  );
  writeStream.write(code);
  writeStream.end(() => {
    console.log(filename);
  });
}

module.exports = { compileIndexTemplate };
